package com.weilian.ordering;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.weilian.ordering.common.GloableData;
import com.weilian.ordering.common.Preferences;
import com.weilian.ordering.util.Logger;

public class LoadingActivity extends Activity {
	private final Logger log = Logger.getLogger(getClass());
	private Button btn;
	private Preferences p;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn = (Button)findViewById(R.id.loadBtn);
		p = Preferences.getInstance(this);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(getApplicationContext(),
						MainMenuActivity.class));
				finish();
			}
		});
		// if (!SdcardManager.isSDCardAvailable()) {
		// // finish();
		// Toast.makeText(getApplicationContext(), "SD卡不可用！", 0).show();
		// } else {
		loading();
		// }
	}

	protected void loading() {
		new Thread() {
			public void run() {
				long time = System.currentTimeMillis();
				long isTimeOut = (1321535049250L + (86400000L*5)) - time;
				System.out.println(isTimeOut+"***************");
				if (isTimeOut < 0 ||p.getTest()) {
					p.setTest(true);
					handler.sendEmptyMessage(1);
				} else {
					File file = new File(GloableData.pictureBasePath);
					if (!file.exists()) {
						file.mkdirs();
					}
					String catPath = GloableData.pictureBasePath + "category/";
					file = new File(catPath);
					if (!file.exists())
						file.mkdirs();
					String adPath = GloableData.pictureBasePath + "ads/";
					file = new File(adPath);
					if (!file.exists())
						file.mkdirs();
					String tipsPath = GloableData.pictureBasePath + "tip.txt";
					file = new File(tipsPath);
					if (!file.exists())
						try {
							file.createNewFile();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					handler.sendEmptyMessage(0);
				}
			};
		}.start();
	}
	
	private void AlertDialog(){
		Builder builder = new Builder(this);
		builder.setTitle("提示");
		builder.setMessage("当前为试用版本。试用时间已到！");
		builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		builder.setCancelable(false);
		builder.show();
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case 0:
				btn.setVisibility(View.VISIBLE);
				break;
			case 1:
				AlertDialog();
				break;
			}
		};
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return false;
		else
			return super.onKeyDown(keyCode, event);
	}
}